package nettyTest.netty3Test.codecTest1.codeTest1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CodeServer {

    private static final Logger logger = LogManager.getLogger(CodeServer.class);

    public static void main(String[] args) throws Exception {

        EventLoopGroup workGroup = new NioEventLoopGroup();
        EventLoopGroup bossGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new MyMessageToMessageCodec2());
                p.addLast(new MyMessageToMessageCodec());
                p.addLast(new CodeServerHandler());
            }
        });


        ChannelFuture channelFuture = serverBootstrap.bind(8023);
        channelFuture.addListener(future -> {
            if (future.isSuccess()) {
                logger.info("future success");
            } else {
                logger.info("future fail");
            }
        });
//        channelFuture.channel().closeFuture().addListener(future -> {
//            logger.info("future close listener");
//        });
//        channelFuture.channel().close();
//        logger.info("future close");
    }
}
